//
//  AlbumController.m
//  PhotoKitDemo
//
//  Created by 红沙尘 on 17/5/3.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "AlbumController.h"
#import "AlbumDataSource.h"
#import "AlbumPreviewVC.h"

@interface AlbumController ()
{
    AlbumDataSource *dataSource;
    CGFloat itemWidth;
}
@end

@implementation AlbumController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    [myCollectionView registerNib:[UINib nibWithNibName:@"AlbumCell" bundle:nil]
       forCellWithReuseIdentifier:@"AlbumCell"];
    
    int     count = 3;
    CGFloat space = 2;
    
    itemWidth = ([UIScreen mainScreen].bounds.size.width - space*(count - 1))/count;
    //边距占5*4=20 ，2个
    //图片为正方形，边长：(fDeviceWidth-20)/2-5-5 所以总高(fDeviceWidth-20)/2-5-5 +20+30+5+5 label高20 btn高30 边
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing      = space;
    flowLayout.minimumInteritemSpacing = space;
    flowLayout.itemSize                = CGSizeMake(itemWidth, itemWidth);
    flowLayout.sectionInset            = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection         = UICollectionViewScrollDirectionVertical;
    
    [myCollectionView setCollectionViewLayout:flowLayout];

    dataSource = [AlbumDataSource shareAlbumDataSource:^{
        dispatch_sync(dispatch_get_main_queue(), ^{
            [myCollectionView reloadData];
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return dataSource.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    AlbumCell *cell = (AlbumCell *)[cv dequeueReusableCellWithReuseIdentifier:@"AlbumCell"
                                                                       forIndexPath:indexPath];
    
    CGSizeMake(dataSource.assets[indexPath.row].pixelWidth, dataSource.assets[indexPath.row].pixelHeight);
    [[PHCachingImageManager defaultManager] requestImageForAsset:dataSource.assets[indexPath.row]
                                                      targetSize:CGSizeMake(itemWidth*2, itemWidth*2)
                                                     contentMode:PHImageContentModeAspectFill
                                                         options:dataSource.options
                                                   resultHandler:^(UIImage * _Nullable result,
                                                                   NSDictionary * _Nullable info)
     {
         cell.photoImageView.image = result;
     }];
    
    cell.backgroundColor = [UIColor colorWithRed:(arc4random()%100 + 155)/255.0f
                                           green:(arc4random()%100 + 155)/255.0f
                                            blue:(arc4random()%100 + 155)/255.0f
                                           alpha:1];
    
    return cell;
}

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumPreviewVC *preview = [[AlbumPreviewVC alloc] init];
    preview.dataSource = dataSource;
    [self.navigationController pushViewController:preview animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
